﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

using System.Web;
using System.Net;


namespace FacebookTestUserManager
{
    public partial class ManagerWindow : Window
    {
        public string AccessToken { set; get; }
        public string AppId { set; get; }

        public ManagerWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ManagerWindow_Loaded);
        }

        void ManagerWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(AccessToken) || string.IsNullOrWhiteSpace(AppId))
            {
                MessageBox.Show("AccessToken or AppId are invalid.");
                this.Close();
                return;
            }

            var helper = new FacebookHelper(this.AppId, this.AccessToken);

            try
            {
                dynamic apps = helper.GetAppsInformation();
                this.appName.Text = apps["name"];
                BindUsers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }


        }

        private void BindUsers()
        {
            var helper = new FacebookHelper(this.AppId, this.AccessToken);
            var users = helper.GetTestUsers();
            this.dataGrid1.DataContext = users;
        }

        private void newTester_Click(object sender, RoutedEventArgs e)
        {
            var newTesterWindow = new NewUserWindow() { AccessToken = this.AccessToken, AppId = this.AppId };
            newTesterWindow.Closing += new System.ComponentModel.CancelEventHandler(newTesterWindow_Closing);
            newTesterWindow.ShowDialog();

        }

        void newTesterWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                BindUsers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void OnHyperlinkClick(object sender, RoutedEventArgs e)
        {
            var destination = ((Hyperlink)e.OriginalSource).NavigateUri;
            FacebookHelper.NavigateTo(destination.ToString());

            BindUsers();
        }

        void OnProfileHyperlinkClick(object sender, RoutedEventArgs e)
        {

            var destination = ((Hyperlink)e.OriginalSource).NavigateUri;
            FacebookHelper.NavigateTo(string.Format("http://www.facebook.com/apps/application.php?id={0}", this.AppId));

            BindUsers();
        }

        private void deleteTester_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this Test user?","Delete", MessageBoxButton.YesNo) == MessageBoxResult.No)
            {
                return;
            }

            var id = (sender as Button).DataContext.ToString();

            var helper = new FacebookHelper(this.AppId, this.AccessToken);
            try
            {
                helper.DeleteTestUser(id);
                BindUsers();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void updateList_Click(object sender, RoutedEventArgs e)
        {
            BindUsers();

        }

        private void friendsSetting_Click(object sender, RoutedEventArgs e)
        {
            var idAndToken = (sender as Hyperlink).DataContext as string;
            var splited = idAndToken.Split(',');

            var id = splited[0];
            var userToken = splited[1];
            var friends = (this.dataGrid1.DataContext as List<TestUser>).Where(u=>u.id!=id).ToList();


            try
            {
                var friendSetting = new FriendSettingWindow()
                {
                    AppId = this.AppId,
                    AccessToken = this.AccessToken,
                    UserId = id,
                    UserAccessToken = userToken,
                    Friends = friends
                };

                friendSetting.Show();
                BindUsers();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
                this.Close();
            }


        }

        private void deleteAllTester_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete all Test users?", "Delete", MessageBoxButton.YesNo) == MessageBoxResult.No)
            {
                return;
            }

            var helper = new FacebookHelper(this.AppId, this.AccessToken);

            foreach (var tester in dataGrid1.DataContext as List<TestUser>)
            {
                helper.DeleteTestUser(tester.id);
            }

            BindUsers();

        }

        private void showCorrelation_Click(object sender, RoutedEventArgs e)
        {
            var correlationWindow = new CorrelationWindow()
            {
                AccessToken = this.AccessToken,
                AppId = this.AppId
            };

            correlationWindow.Show();
        }




    }
}
